//
//  Alerts.m
//  Reminders
//
//  Created by Nearsoft User on 8/28/12.
//
//

#import "Alerts.h"

@implementation Alerts

@synthesize localNotif;
@synthesize alertIsShown;
@synthesize toDoView;
@synthesize doneView;

#pragma mark - Alert Methods
/**
 * Method name: showAlert
 * Description: Shows the an alert of the received notification
 * Parameters:
 */
- (void)showAlert
{
    // Ifthere is an alert already being shown
    if (alertIsShown == NO)
    {
        // Set the alert ith the notification data
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Remember To:"
                              message:localNotif.alertBody
                              delegate:self
                              cancelButtonTitle:@"View Details"
                              otherButtonTitles:@"Close", nil];
        // Show the alert
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (notifView == nil)
        notifView = [[ReminderModalDetailView alloc] initWithNibName:@"ReminderModalDetailView" bundle:[NSBundle mainBundle]];

    // If the "View Details" button was pressed on the alert
    if (buttonIndex == 0)
    {
        // Show a modal view with the reminder data
        [self showModalDetailView];
    }
    else
    {
        // Reset the alert flag and update the reminder from the database
         notifView.reminderPk = [[NSString stringWithFormat:@"%@", [localNotif.userInfo valueForKey:@"id"]] intValue];
        [notifView updateReminder];
        [self passAlertShown];
    }
}

- (void)willPresentAlertView:(UIAlertView *)alertView
{
    self.alertIsShown = YES;
}

/**
 * Method name: passAlertShown
 * Description: Sets the alert flag to NO and reload the views
 * Parameters:
 */
- (void)passAlertShown
{
    self.alertIsShown = NO;

    [toDoView reloadTable];
    [doneView reloadTable];

    localNotif = nil;
    notifView = nil;
}

#pragma mark - View Method
/**
 * Method name: showModalDetailView
 * Description: Shows a modal view with the remainder details
 * Parameters:
*/
- (void)showModalDetailView
{
    if (notifView == nil)
        notifView = [[ReminderModalDetailView alloc] initWithNibName:@"ReminderModalDetailView" bundle:[NSBundle mainBundle]];

    notifView.delegate = self;
    notifView.reminderPk = [[NSString stringWithFormat:@"%@", [localNotif.userInfo valueForKey:@"id"]] intValue];

    if ([notifView getReminderData])
        [toDoView presentModalViewController:notifView animated:YES];
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]
                                   initWithTitle:@"Remember Missing:"
                                   message:@"The reminder doens't exist anymore"
                                   delegate:nil
                                   cancelButtonTitle:@"Close"
                                   otherButtonTitles:nil];
        [alert show];
        localNotif = nil;
    }
}

@end
